package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemPostService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    @Autowired
    private SystemPostService systemPostService;




    @Override
    public SystemUserItemVo listSystemUserItemVoById(Long id) {
        SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
        SystemUser systemUser = baseMapper.selectById(id);
        systemUser.setPassword(null);
        BeanUtils.copyProperties(systemUser, systemUserItemVo);

        SystemPost post = systemPostService.getById(systemUser.getPostId());
        systemUserItemVo.setPostName(post.getName());
        return systemUserItemVo;
    }

    @Override
    public IPage<SystemUserItemVo> listSystemUserItemVoByPage(Page<SystemUser> page, SystemUserQueryVo queryVo) {

        QueryWrapper<SystemUser> systemUserQueryWrapper = new QueryWrapper<>();
        String name = queryVo.getName();
        String phone = queryVo.getPhone();
        // 模糊查询
        systemUserQueryWrapper.like(StringUtils.hasText(name),"name",name);
        systemUserQueryWrapper.like(StringUtils.hasText(phone),"phone",phone);
        // 基本字段信息，缺少岗位名称---postName
        Page<SystemUser> systemUserPage = baseMapper.selectPage(page, systemUserQueryWrapper);
        // 查询system_post表中所有信息
        List<SystemPost> systemPosts = systemPostService.list();
        // 转化为map,其中key为postid，value为postname
        Map<Long, String> postMap = systemPosts.stream().collect(Collectors.toMap(SystemPost::getId, SystemPost::getName));
        // 获取基本信息集合
        List<SystemUser> systemUsers = systemUserPage.getRecords();
        // 创建SystemUserItemVo集合
        List<SystemUserItemVo> systemUserItemVos = new ArrayList<>();
        // 封装postname数据
        for (SystemUser systemUser : systemUsers) {
             // 创建SystemUserItemVo对象接受postname
            SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
            BeanUtils.copyProperties(systemUser, systemUserItemVo);
            // 设置postname
            systemUserItemVo.setPostName(postMap.get(systemUser.getPostId()));
            systemUserItemVos.add(systemUserItemVo);
        }
        // 重新封装IPage<SystemUserItemVo>数据
        IPage<SystemUserItemVo> pageResult = new Page<>();
        pageResult.setPages(systemUserPage.getPages());
        pageResult.setSize(systemUserPage.getSize());
        pageResult.setTotal(systemUserPage.getTotal());
        pageResult.setRecords(systemUserItemVos);

        return pageResult;
    }


}




